<?php
    // load library
include("config.php");
ini_set('display_errors', 'On');
require_once('classes/class.jira.php');
require_once('classes/class.phpmailer.php');
require_once 'Zend/Loader.php';
    Zend_Loader::loadClass('Zend_Gdata');
    Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
    Zend_Loader::loadClass('Zend_Gdata_Calendar');
    Zend_Loader::loadClass('Zend_Http_Client');
    
    
	function getuser($jirauser,$jirapass,$project,$host){
		$config->username= $jirauser;
		$config->password= $jirapass;
		$config->port= "";
		$config->host= $host;
		$query->project = 'REC';

		$queryIssue = new Jira($config);
		$array=$queryIssue->queryUser($query);
		$response = json_decode($array->responseBody);
		return $response;
		}	
	//setting the date range
	$date = date('Y-m-d');
	$newdate =Date('Y-m-d', strtotime("+1 days"));
	
   
	
	//getting all the users in project
	$user=getuser($jirauser,$jirapass,$project,$host);
	foreach($user as $key){
	
		if($key->name!=="careers"){
			 // create authenticated HTTP client for Calendar service
			 $gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
			$user = $gmailuser;
			$pass = $gmailpass;
			$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
			$gcal = new Zend_Gdata_Calendar($client);
			$query = $gcal->newEventQuery();
			$query->setUser('default');
			$query->setVisibility('private');
			$query->setProjection('basic');
			$query->setStartMin($date);
			$query->setStartMax($newdate);
			$query->setOrderby('starttime');
			$query->setSortOrder('a');
			$query->setQuery(''.$key->name.'@eyepax.com');
			$b=0;
			try {
			  $feed = $gcal->getCalendarEventFeed($query);
			} catch (Zend_Gdata_App_Exception $e) {
			  echo "Error: " . $e->getResponse();
			}
			if(count($feed)>0){
				$a="";
				foreach($feed as $event){
					$a.='<hr><h2>'.stripslashes($event->title).'</h2><p>'.stripslashes($event->content).'</p>';
				}
				$body= '<p>Hi '.$key->displayName.'</p><p>You have '.$feed->totalResults.' Interviews scheduled for the day.Please find more info below.</p>'.$a.'';
				$mail= new PHPMailer(true);
				$mail->SingleTo = true;
				$mail->AddReplyTo($AddReplyTo);
				$mail->SetFrom($gmailuser,"HR - Eyepax IT Consulting");
				$address = ''.$key->name.'@eyepax.com';
				$mail->AddAddress($address, $fromname);
				$mail->Subject    = $cronsubject;
				$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
				$mail->MsgHTML($body);
				if(!$mail->Send()) {
				  echo "Mailer Error: " . $mail->ErrorInfo;
				} else {
				  echo "Message sent!";
				}
				echo '<br>#########################'.$key->name.'####################################<br>';
				echo $feed->totalResults." Total results found<br>";
				foreach ($feed as $event) {
				  echo "<li>\n";
				  echo "<h2>" . stripslashes($event->title) . "</h2>\n";
				  echo stripslashes($event->summary) . " <br/>\n";
				  echo "</li>\n";
				}
				echo "</ul>";
				
			}
		}
	
	}
		
	
    ?>